import 'package:english_words/english_words.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:ninepatch_image/ninepatch_image.dart';
import 'package:ui_demo/util/print_util.dart';

class _MyScrollBehavior extends MaterialScrollBehavior {
  @override
  Set<PointerDeviceKind> get dragDevices => {
        PointerDeviceKind.touch,
        PointerDeviceKind.mouse,
      };

  @override
  Widget buildScrollbar(
      BuildContext context, Widget child, ScrollableDetails details) {
    // When modifying this function, consider modifying the implementation in
    // the base class ScrollBehavior as well.
    switch (axisDirectionToAxis(details.direction)) {
      case Axis.horizontal:
        return Scrollbar(
          controller: details.controller,
          child: child,
        );
      case Axis.vertical:
        switch (getPlatform(context)) {
          case TargetPlatform.linux:
          case TargetPlatform.macOS:
          case TargetPlatform.windows:
            assert(details.controller != null);
            return Scrollbar(
              controller: details.controller,
              child: child,
            );
          case TargetPlatform.android:
          case TargetPlatform.fuchsia:
          case TargetPlatform.iOS:
            return child;
        }
    }
  }
}

class ListViewPage extends StatefulWidget {
  const ListViewPage({super.key});

  @override
  State<ListViewPage> createState() => _ListViewPageState();
}

class _ListViewPageState extends State<ListViewPage>
    with SingleTickerProviderStateMixin {
  static const mTAG = "_ListViewPageState";
  late List<String> wordList = [];
  late ScrollController mScrollController;
  late ScrollPosition position;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    for (int i = 0; i <= 100; i++) {
      wordList.add(WordPair.random().asString);
    }
    mScrollController = ScrollController();
    // position = mScrollController.createScrollPosition(ScrollPhysics(), this, mScrollController.position);
    mScrollController.addListener(() {
      LogUtils.d(
        mTAG,
        "initState${mScrollController.offset}",
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    //   return ScrollConfiguration(
    //       behavior: _MyScrollBehavior(),
    //       child: ListView.builder(
    //           physics: const AlwaysScrollableScrollPhysics(),
    //           controller: mScrollController,
    //           scrollDirection: Axis.horizontal,
    //           itemCount: wordList.length,
    //           itemBuilder: (context, index) {
    //             return const Padding(
    //               padding: EdgeInsets.all(10),
    //               child:NinePatchImage( imageProvider: AssetImage("assets/dot_nine/bubble_blue.9.png"),
    //                   child: Text( "Lorem Ipsum is simply dummy text of the printing ")),
    //             );
    //           }));
    // }
    return Listener(
      onPointerSignal: (event) {
        LogUtils.d(
          mTAG,
          "build$event",
        );
        LogUtils.d(
          mTAG,
          "build${event.position.dy}",
        );
        LogUtils.d(
          mTAG,
          "old position${mScrollController.offset}",
        );
        // LoggerUtil.d(mTAG, "event.delta${event.delta.dx}",);
        LogUtils.d(
          mTAG,
          "event.delta${event.delta}",
        );
        LogUtils.d(
          mTAG,
          "event.${event.distance}",
        );
        if (event is PointerScrollEvent) {
          LogUtils.d(
            mTAG,
            "scrollDelta${event.scrollDelta}",
          );
          LogUtils.d(
            mTAG,
            "scrollDelta.dy.${event.scrollDelta.dy}",
          );
          LogUtils.d(
            mTAG,
            "scrollDelta.dx${event.scrollDelta.dx}",
          );
          var position = mScrollController.offset + event.scrollDelta.dy * 2;
          LogUtils.d(
            mTAG,
            "position$position",
          );
          mScrollController.animateTo(position,
              duration: const Duration(milliseconds: 1000), curve: Curves.ease);
        }
      },
      child: ListView.builder(
          controller: mScrollController,
          scrollDirection: Axis.horizontal,
          itemCount: wordList.length,
          itemBuilder: (context, index) {
            return Padding(
              padding: const EdgeInsets.all(10),
              child: NinePatchImage(
                  imageProvider:
                      const AssetImage("assets/dot_nine/bubble_blue.9.png"),
                  child: Text(wordList[index])),
            );
          }),
    );
  }
}
